﻿using System;
using System.Reactive.Linq;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using SharpRomans.Exceptions;

namespace SharpRomans.Tests.ParsingRules.Support
{
	public abstract class RuleTesterBase
	{
		protected void Observe<T>(string literal) where T : IObserver<char>
		{
			// ReSharper disable PossiblyMistakenUseOfParamsMethod
			T subject = (T)Activator.CreateInstance(typeof(T), new[] { literal });
			// ReSharper restore PossiblyMistakenUseOfParamsMethod
			literal.ToObservable().Subscribe(subject);
		}

		protected IResolveConstraint ThrowsWhenParsing(string literal, int index)
		{
			char character = literal[index];
			return Throws.InstanceOf<ParseException>()
				.With.Property("CharacterNumber").EqualTo(index).And
				.With.Property("Character").EqualTo(character).And
				.With.Message.StringContaining(asChar(character)).And
				.With.Message.StringContaining(literal);
		}

		private string asChar(char character)
		{
			return "'" + character + "'";
		}
	}
}
